package com.android.takegism.utils;

import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.Set;

public class CommandUtils {

    private CommandUtils() {
        this.encodeCommandsMap = new HashMap<>();
        this.PlotsCommands = new HashMap<>();
        this.CluesCommands = new HashMap<>();

//        this.CluesCommandsValues = (Set<String>) CluesCommands.values();
//        this.PlotsCommandsValues = (Set<String>) PlotsCommands.values();

        encodeCommandsMap.put("关灯", "#LightOff#");
        encodeCommandsMap.put("开灯", "#LightOn#");
        encodeCommandsMap.put("开闪光灯", "#FlashLightOn#");
        encodeCommandsMap.put("关闪光灯", "#FlashLightOff#");

        encodeCommandsMap.put("成功", "#Vectory#");
        encodeCommandsMap.put("失败", "#Defeat#");

        for (int i = 0; i < 5; i++) {
            encodeCommandsMap.put("剧情"+i, "#Plot"+i+"#");
            PlotsCommands.put("剧情"+i, "#Plot"+i+"#");
        }


        for (int i = 0; i < 15; i++) {
            encodeCommandsMap.put("线索" + i, "#Clue" + i + "#");

            CluesCommands.put("线索" + i, "#Clue" + i + "#");
        }


    }

    private static CommandUtils instance = new CommandUtils();

    public static CommandUtils getInstance() {
        return instance;
    }

    private HashMap<String, String> encodeCommandsMap;
    private HashMap<String, String> PlotsCommands;
    private HashMap<String, String> CluesCommands;
    private Set<String> PlotsCommandsValues;
    private Set<String> CluesCommandsValues;


    public HashMap<String, String> getEncodeCommandsMap() {
        return encodeCommandsMap;
    }

    public void encodeCommand(@Nullable String s, Clue clue) {
        if (clue == null) {
            sendMsg(s);
        } else {
            // 此时 s 没有用
            String s1 = encodePuzzleCommand(clue);
            sendMsg(s1);
        }
    }

    private void sendMsg(String s) {
        // 没有实现
        throw new RuntimeException("等待CHH实现");
    }

    public static String encodePuzzleCommand(Clue clue) {
        StringBuffer sb = new StringBuffer();
        sb.append("#Puzzle#{");
        sb.append(clue.getImageId() + ",")
                .append(clue.getClue() + "}");
        String s = sb.toString();
        System.out.println(s);
        return s;
    }


    private void lightOn() {
        // 没有实现
        throw new RuntimeException("等待CHH实现");
    }

    private void lightOff() {
        // 没有实现
        throw new RuntimeException("等待CHH实现");
    }

    // 不可以私自调用
    private Clue executeCommand(@Nullable String cmd, Clue clue) {
        if (clue == null) {
            // 普通命令
            switch (cmd) {
                case "LightOn":
                    lightOn();
                    break;
                case "LightOff":
                    lightOff();
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + cmd);
            }
        }
        return clue;
    }


    /**
     * 如果返回null，说明是错误的指令，否则返回字符串或者是Clue
     * @param s
     * @return
     */
    public Object decodeCommand(String s) {
        int i = s.indexOf('#');
        if (i == -1) {
            //
            return null;
        } else {
            int j = s.indexOf('#', i + 1);
            if (j == -1) {
                System.out.println("输入不可以只有一个#，错误了错误了");
                return null;
            }
            String cmd = s.substring(i + 1, j);
            System.out.println("cmd " + cmd);

            return cmd;
//
//            i = s.indexOf('{', j + 1);
//            j = s.indexOf(',', i + 1);
//            int id = Integer.parseInt(s.substring(i + 1, j));
//            System.out.println("id " + id);
//            int k = s.indexOf('}', j + 1);
//            String clueString = s.substring(j + 1, k);
//            System.out.println("clue " + clueString);
//            return new Clue(id, clueString);
        }
    }
}
